package com.biostime.terminal.util;

import java.util.LinkedList;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;


public class RootVO extends BaseElement {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5903246720206546439L;
	private static Log log = LogFactory.getLog(RootVO.class);

	public boolean addElement(BaseElement baseXmlElement) {
		if ((baseXmlElement instanceof Head) || (baseXmlElement instanceof Properties)) {			
			for(int i = getElements().size()-1;i >=0;i--){ 
				if ((baseXmlElement instanceof Head) && (getElements().get(i) instanceof Head)){
					getElements().remove(i);
					break;
				}
				
				if ((baseXmlElement instanceof Properties) && (getElements().get(i) instanceof Properties)){
					getElements().remove(i);
					break;
				}				
			}

		}
		getElements().addElement(baseXmlElement);
		return true;
	}

	public boolean delElement(BaseElement baseXmlElement) {
		if (getElements().size() != 0){
			return getElements().removeElement(baseXmlElement);
		}
		return false;
	}

	/**
	 * 返回RootVO节点下的Head元素。
	 * 
	 * @return Head元素
	 */
	public Head getHead(){
		for (int i = 0; i < getElements().size(); i ++){
			if (getElements().get(i) instanceof Head) {
				return (Head)getElements().get(i);
			}
		}
		return null;
	}
	
	/**
	 * 返回RootVO节点下的所有ArrayList元素。
	 * 
	 * @return 所有ArrayList元素
	 */
	public ArrayLists[] getArrayLists(){
		Vector v = new Vector();
		for (int i = 0; i < getElements().size(); i ++){
			if (getElements().get(i) instanceof ArrayLists) {
				v.addElement((ArrayLists)getElements().get(i));
			}
		}
		return (ArrayLists[]) v.toArray(new ArrayLists[0]);
	}
	
	/**
	 * 返回RootVO节点下的所有LinkedList元素。
	 * 
	 * @return 所有LinkedList元素
	 */
	public LinkedList[] getLinkedLists(){
		Vector v = new Vector();
		for (int i = 0; i < getElements().size(); i++){
			if (getElements().get(i) instanceof LinkedList){
				v.addElement((LinkedList)getElements().get(i));
			}
		}
		return (LinkedList[]) v.toArray(new LinkedList[0]);
	}
	
	/**
	 * 返回RootVO节点下的所有VO元素。
	 * 
	 * @return 所有VO元素
	 */
	public VO[] getVOs(){
		Vector v = new Vector();
		for (int i = 0; i < getElements().size(); i++){
			if (getElements().get(i) instanceof VO){
				v.addElement((VO)getElements().get(i));
			}
		}
		return (VO[]) v.toArray(new VO[0]);
	}
	
	/**
	 * 返回RootVO节点下的Properties元素。
	 * 
	 * @return Properties元素
	 */
	public Properties getProperties(){
		for (int i = 0; i < getElements().size(); i++){
			if (getElements().get(i) instanceof Properties){
				return (Properties)getElements().get(i);
			}
		}
		return null;
	}
	
	/**
	 * @return 返回当前XML节点的Element对象
	 */
	public Element toElement() {
		Element root = new Element("rootVo");
		for (int i = 0; i < getElements().size(); i++){
			root.addContent(((BaseElement)getElements().get(i)).toElement());
		}
		return root;
	}
	
	/**
	 * @return 返回当前XML节点的Document对象
	 */
	public Document toDocument() {
		return new Document(toElement());
	}

	/**
	 * @return 返回当前XML节点的String对象，用于测试验证
	 */
	public String toString() {
		log.debug("[rootVo]");
		return XMLWriter.writeString(toElement());
	}

	/**
	 * 把RootVO转换为Document，并以字节数组的形式返回，方便保存到文件中。
	 * 
	 * @return RootVO的字节数组形式
	 */
	public byte[] toByteArray() {
		return XMLWriter.writeByteArray(new Document(toElement()));
	}

}